//
//  SetViewController.m
//  SHShopping
//
//  Created by 坤鼎 on 2019/9/2.
//  Copyright © 2019 kunding. All rights reserved.
//

#import "SetViewController.h"

@interface SetViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSDictionary *dicData;

@end

@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";

    if ([UserDefaults boolForKey:UserDefaultsPlayInAllNet]) {
        self.dicData = @{@"title":@[
                                 @"视频自动播放",
                                 @"清理APP缓存",
                                 @"关于APP"],
                         @"detailTitle":@[
                                 @"全部网络",
                                 @"",
                                 @""]
                         };
    } else {
        self.dicData = @{@"title":@[
                                 @"视频自动播放",
                                 @"清理APP缓存",
                                 @"关于APP"],
                         @"detailTitle":@[
                                 @"仅Wi-Fi",
                                 @"",
                                 @""]
                         };
    }
    
    [self.view addSubview:self.tableView];
    
    UIButton *btnBottom = [[UIButton alloc] initWithFrame:CGRectMake(27, SCREEN_HEIGHT - 54 - 44 - kNavBarAndStatusBarHeight, SCREEN_WIDTH - 27 * 2, 44)];
    [self.view addSubview:btnBottom];
    
    [btnBottom setTitle:@"退出登录" forState:UIControlStateNormal];
    [btnBottom gradientViewColorArray:@[] pointArr:@[]];
    [btnBottom addTarget:self action:@selector(actionBottom) forControlEvents:UIControlEventTouchUpInside];
    btnBottom.hidden = YES;
}

- (void)actionBottom {
    
    if ([[UserManager sharedUserManager] isLogin]) {
        [SXLoadingView showAlertHUD:@"退出成功" duration:SXLoadingTime];
        [[UserManager sharedUserManager] logout];
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [SXLoadingView showAlertHUD:@"啊哦,您还没有登录哦" duration:SXLoadingTime];
    }
}

#pragma mark --UITableView代理

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    switch (indexPath.row) {
        case 0:{
            UIAlertController *alerController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
            
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"仅Wi-Fi" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [UserDefaults setBool:NO forKey:UserDefaultsPlayInAllNet];
                [UserDefaults synchronize];
                [SXLoadingView showAlertHUD:@"切换成功" duration:SXLoadingTime];
                
                self.dicData = @{@"title":@[
                                         @"视频自动播放",
                                         @"清理APP缓存",
                                         @"关于APP"],
                                 @"detailTitle":@[
                                         @"仅WI-FI",
                                         @"",
                                         @""]};
                [self.tableView reloadData];
            }];
            UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"全部网络" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [UserDefaults setBool:YES forKey:UserDefaultsPlayInAllNet];
                [UserDefaults synchronize];
                [SXLoadingView showAlertHUD:@"切换成功" duration:SXLoadingTime];
                self.dicData = @{@"title":@[
                                         @"视频自动播放",
                                         @"清理APP缓存",
                                         @"关于APP"],
                                 @"detailTitle":@[
                                         @"全部网络",
                                         @"",
                                         @""]};
                [self.tableView reloadData];
            }];
            [alerController addAction:action];
            [alerController addAction:actionCancel];
            [self.navigationController presentViewController:alerController animated:YES completion:nil];
        }
            break;
        case 1:{
            //获取缓存图片的大小(字节)
            NSUInteger bytesCache = [[SDImageCache sharedImageCache] totalDiskSize];
            //换算成 MB (注意iOS中的字节之间的换算是1000不是1024)
            float MBCache = bytesCache /1000 / 1000;
            
            UIAlertController *alerController = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"您有%.1fM缓存可清理", MBCache] preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"清理" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (MBCache == 0) {
                    [SXLoadingView showAlertHUD:@"没什么可清理的哦" duration:SXLoadingTime];
                } else {
                    [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
                        [SXLoadingView showAlertHUD:[NSString stringWithFormat:@"成功清理%.1fM", MBCache] duration:SXLoadingTime];
                    }];
                    [JXBWebViewController clearAllWebCache];
                }
            }];
            UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alerController addAction:action];
            [alerController addAction:actionCancel];
            [self.navigationController presentViewController:alerController animated:YES completion:nil];
        }
            break;
        case 2:{
            [SXLoadingView showAlertHUD:@"早安上海 2.0" duration:SXLoadingTime];
        }
            break;
            
        default:
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"MineViewController";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    cell.textLabel.text = self.dicData[@"title"][indexPath.row];
    cell.textLabel.font = FONT(16);
    
    cell.detailTextLabel.text = self.dicData[@"detailTitle"][indexPath.row];
    cell.detailTextLabel.font = FONT(16);
 
    UIImageView *accessoryImgView = [[UIImageView alloc] initWithImage:ImageNamed(@"箭头")];
    cell.accessoryView = accessoryImgView;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - kNavBarAndStatusBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return _tableView;
}

@end
